package com.tywj.panda.driver

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.lifecycle.Observer
import com.afollestad.materialdialogs.MaterialDialog
import com.alibaba.android.arouter.facade.annotation.Route
import com.autocareai.lib.extension.visible
import com.tywj.lib.core.common.extension.loadRound
import com.tywj.lib.core.common.extension.onClick
import com.tywj.lib.core.common.util.PhoneUtil
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.route.RouteUtil
import com.tywj.panda.customer.common.entity.UserEntity
import com.tywj.panda.customer.common.event.UserEvent
import com.tywj.panda.customer.common.http.PDObjectCallback
import com.tywj.panda.customer.common.manager.AuthorityManager
import com.tywj.panda.customer.common.provider.IUserCenterService
import com.tywj.panda.customer.common.tool.LogoutTool
import com.tywj.panda.customer.common.view.base.MyBaseFragment
import com.tywj.panda.customer.common.view.dialog.showByCZConfig
import com.tywj.panda.customer.usercenter.api.UserCenterApi
import com.tywj.panda.customer.usercenter.cache.UserCenterCache
import com.tywj.panda.customer.usercenter.settings.TipsDialog
import com.tywj.panda.driver.route.DriverRoute
import kotlinx.android.synthetic.main.driver_fragment_user.*


/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/06/20
 *     desc   : 我的(个人中心)
 *     version: 1.0.0
 * </pre>
 */
@Route(path = DriverRoute.DRIVER_USER)
class DriverUserFragment : MyBaseFragment() {
    override fun getLayoutID(): Int = R.layout.driver_fragment_user

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

    }

    override fun initListener() {
        super.initListener()

        UserEvent.loadUserDataSuccessEvent.observe(this, Observer {
            // 用户信息发生变化，重新请求用户信息
            loadUserInfo(it)
        })


        clCenter.onClick {
            RouteUtil.getServiceProvider(IUserCenterService::class.java)?.toUserCenterInfo()?.navigation(this)
        }

        flDriverSettings.onClick {
            RouteUtil.getServiceProvider(IUserCenterService::class.java)?.toSetting()?.navigation(this)
        }

        flTicket.onClick {
            DriverRoute.toCheckCode().navigation(this)
        }
        flFeedBack.onClick {
            RouteUtil.getServiceProvider(IUserCenterService::class.java)?.toUserFeedBack()?.navigation(this)
        }

        flServiceCenter.onClick {

            TipsDialog.Builder(this)
                .setTipDetails(ResourcesUtil.getString(R.string.common_prompt_call_customer_service_message))
                .setConfirmListener {
                    PhoneUtil.call(requireContext(), ResourcesUtil.getString(R.string.common_customer_service_phone))
                }
                .show()
        }
    }

    override fun onVisibleFirst() {
        super.onVisibleFirst()

        loadUserInfo(AuthorityManager.uid)
    }

    private fun showPromptCallCustomerServiceDialog() {
        MaterialDialog(requireContext())
            .title(R.string.common_prompt_title)
            .message(R.string.common_prompt_call_customer_service_message)
            .negativeButton(R.string.common_negative)
            .positiveButton(R.string.common_positive) {
                PhoneUtil.call(requireContext(), ResourcesUtil.getString(R.string.common_customer_service_phone))
            }
            .showByCZConfig()
    }

    @SuppressLint("SetTextI18n")
    private fun loadUserInfo(uid: String) {
        showLoadingDialog()

        UserCenterApi.getUserDetails(uid).attachToLifecycle(this)
            .execute(object : PDObjectCallback<UserEntity>(UserEntity::class.java) {
                override fun success(data: UserEntity) {

                    tvUserName.text = if (data.nickName.isNullOrEmpty()) {
                        ResourcesUtil.getString(R.string.user_center_set_nickname)
                    } else {
                        data.nickName
                    }
                    data.avatar?.let {
                        ivHeader.loadRound(it, R.drawable.common_portrait_default)
                    }
                    tvUserId.text = "ID:${data.uid}"
                    visible(tvUserId)

                    UserCenterCache.userInfo = data
                }

                override fun error(code: Int, message: String) {
                    super.error(code, message)

                    when (code) {
                        -4, 401 -> {
                            LogoutTool.logout(R.string.common_account_login_token_is_expired)
                        }
                    }
                }

                override fun onComplete() {
                    super.onComplete()
                    dismissLoadingDialog()
                }
            })
    }
}